﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BuildServer.Models;

namespace BuildServer.Controllers
{
    [Authorize]
    public class TemplateController : Controller
    {
        private BuildServerDbContext db = new BuildServerDbContext();

        //
        // GET: /Template/

        public ActionResult Index()
        {
            db.Database.Log = (msg) => System.Diagnostics.Trace.WriteLine(msg);
            return View(db.BuildTemplates);
        }

        //
        // GET: /Template/Details/5

        public ActionResult Details(int id = 0)
        {
            BuildTemplate buildtemplate = db.BuildTemplates.Find(id);
            if (buildtemplate == null)
            {
                return HttpNotFound();
            }
            return View(buildtemplate);
        }

        //
        // GET: /Template/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Template/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(BuildTemplate buildtemplate)
        {
            if (ModelState.IsValid)
            {
                db.BuildTemplates.Add(buildtemplate);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(buildtemplate);
        }

        //
        // GET: /Template/Edit/5

        public ActionResult Edit(int id = 0)
        {
            BuildTemplate buildtemplate = db.BuildTemplates.Find(id);
            if (buildtemplate == null)
            {
                return HttpNotFound();
            }
            return View(buildtemplate);
        }

        //
        // POST: /Template/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(BuildTemplate buildtemplate)
        {
            if (ModelState.IsValid)
            {
                db.Entry(buildtemplate).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(buildtemplate);
        }

        //
        // GET: /Template/Copy/5

        public ActionResult Copy(int id = 0)
        {
            BuildTemplate buildtemplate = db.BuildTemplates.Find(id);
            if (buildtemplate == null)
            {
                return HttpNotFound();
            }
            return View(buildtemplate);
        }

        //
        // POST: /Template/Copy/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Copy(BuildTemplate buildtemplate, int source_id = 0, bool deepcopy = true)
        {
            if (ModelState.IsValid)
            {
                db.Database.Log = (msg) => System.Diagnostics.Trace.WriteLine(msg);
                
                // create new build template
                var newTemplate = db.BuildTemplates.Create();
                newTemplate.Name = buildtemplate.Name;
                newTemplate.WorkingDirectory = buildtemplate.WorkingDirectory;
                newTemplate.BuildProps = buildtemplate.BuildProps;
                newTemplate.BuildScript = buildtemplate.BuildScript;
                db.BuildTemplates.Add(newTemplate);

                // copy build definitions
                if (deepcopy)
                {
                    // load definitions with properties
                    var definitions = db.BuildDefinitions.Include(x => x.BuildProperties).Where(x => x.BuildTemplateId == source_id).ToList();
                    foreach (var def in definitions)
                    {
                        // copy definition
                        var newDef = db.BuildDefinitions.Create();
                        newDef.Name = def.Name + " (Copy)";
                        newDef.BuildTemplate = newTemplate;
                        db.BuildDefinitions.Add(newDef);

                        // copy properties
                        foreach(var prop in def.BuildProperties)
                        { 
                            var newProp = db.BuildProperties.Create();
                            newProp.Name = prop.Name;
                            newProp.Value = prop.Value;
                            newProp.BuildDefinition = newDef;
                            db.BuildProperties.Add(newProp);
                        }
                    }
                }
                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(buildtemplate);
        }

        //
        // GET: /Template/Delete/5

        public ActionResult Delete(int id = 0)
        {
            BuildTemplate buildtemplate = db.BuildTemplates.Find(id);
            if (buildtemplate == null)
            {
                return HttpNotFound();
            }
            return View(buildtemplate);
        }

        //
        // POST: /Template/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            BuildTemplate buildtemplate = db.BuildTemplates.Find(id);
            db.BuildTemplates.Remove(buildtemplate);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}